﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Net.Mail;

public partial class user_profile : System.Web.UI.Page
{
    int no;
    protected void Page_Load(object sender, EventArgs e)
    {
        no = int.Parse(Request["no"]);

        Response.Redirect("my/index.aspx?no=" + no.ToString());


        if (Request["follow"] != null)
            this.follow();

        if (this.Session["userno"] == null)
        {
            this.Panel1.Visible = false;

        }
        else
        {
            this.LinkButton1.Visible = false;
        }

        ctpDataContext db = new ctpDataContext();
        user u1 = db.users.Single(p => p.no == no);

        string nickname = u1.nickname.Trim();

        this.Label1.Text = nickname;
        //this.Label2.Text = nickname;
        this.Label3.Text = nickname;

        this.LinkButton1.Text = "Login to leave " + nickname + " a message";
        this.Label9.Text = "Leave " + nickname + " a message";

        this.LinkButton2.Text = "Follow "+nickname;
        this.Label10.Text = "You can share things reviewed by " + nickname+" through this follow relationsip.";

        this.Title = "Citiport - " + nickname + " - travel, living, friendship";


        if(u1.profile_picture==null)
            this.Image2.Visible=false;
        else
            this.Image2.ImageUrl="user_imgs/2/"+u1.profile_picture.Trim();


        var reviewed = from p in db.ct_s_reviews
                       join q in db.ct_s_profiles on p.ct_s_no equals q.no
                       where p.create_user_no == no && q.valid==true
                       orderby p.addtime
                       select new
                       {
                           ct_name=q.ct_profile.title.Trim(),
                          ranking=p.ranking,
                          no= p.ct_s_no,
                          main_pic_fn= q.main_pic_fn.Trim(),
                           title=q.title,
                           comment=p.comment,
                           addtime=p.addtime
                     
                           
                       };



        //var created = from p in db.ct_s_profiles
        //              where p.create_user_no == no
        //              select p;

        this.DataList2.DataSource = reviewed.Distinct();


        var follower = from p in db.user_follows
                       join q in db.users on p.create_user_no equals q.no
                       where p.follow_who == no
                       orderby p.addtime descending
                       select new
                       {
                           nickname=q.nickname,
                           profile_picture=q.profile_picture,
                           no=p.create_user_no

                       };

        //foreach (user_follow u in follower)
        //{

        //    if (u.user.profile_picture == null)
        //        u.user.profile_picture = "coming.gif";
        //    else
        //        u.user.profile_picture = u.user.profile_picture.Trim();

        //}

        this.Label11.Text = follower.Count().ToString() + " people are following " + nickname;

        this.DataList3.DataSource= follower;

        this.DataBind();

        //this.Label4.Text = created.Count().ToString();
        this.Label5.Text = reviewed.Count().ToString();

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "user_profile.aspx?no=" + no);

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(this.TextBox1.Text.Trim()=="") return;

        ctpDataContext db=new ctpDataContext();
        user_msg msg=new user_msg();
        msg.create_user_no=(int)Session["userno"];
        msg.addtime=DateTime.Now;
        msg.message=TextBox1.Text;
        msg.to_user_no=no;

        db.user_msgs.InsertOnSubmit(msg);
        db.SubmitChanges();

        user fromuser= db.users.Single(p => p.no == (int)Session["userno"]);
        user touser = db.users.Single(p => p.no == (int)no);

        string nickname=fromuser.nickname.Trim();

        string title=nickname+" sent you a message from citiport";
        string message = title + "\n------------\n\n" + this.TextBox1.Text + "\n-------------\nto reply this message, follow the link below:\nhttp://www.citiport.net/user/message.aspx";

      //  System.Web.Mail.SmtpMail.Send("service@citiport.net",touser.email, title, message);

        System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient("210.66.81.108");
        
        MailAddress from = new MailAddress("service@citiport.net");
        MailAddress to = new MailAddress(touser.email);
        MailMessage mmsg = new MailMessage(from, to);
        mmsg.Body = message;
        mmsg.Subject = title;
        //sc.Host = "210.66.81.108";

        try
        {
            sc.Send(mmsg);

            Response.Write("<script language='javascript'>alert('Your message has successfully sent.');window.location.replace('user_profile.aspx?no=" + no + "');</script>");
        }
        catch (Exception e1)
        {
            Response.Write("<script language='javascript'>alert('your message has successfully sent.');window.location.replace('user_profile.aspx?no=" + no + "');</script>");

        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "user_profile.aspx?follow=true&no=" + no);

        this.follow();

    }

    void follow()
    {
        if (no == (int)Session["userno"])
        {
            Response.Write("<script language='javascript'>alert('You can not follow yourself.');window.location.replace('user_profile.aspx?no=" + no + "');</script>");

            return;
        }

        ctpDataContext db = new ctpDataContext();
        user_follow uf = new user_follow();

        uf.addtime = DateTime.Now;
        uf.create_user_no = (int)Session["userno"];
        uf.follow_who = no;

        db.user_follows.InsertOnSubmit(uf);
        db.SubmitChanges();


        user touser = db.users.Single(p => p.no == (int)no);
        user fromuser = db.users.Single(p => p.no == (int)Session["userno"]);

        string nickname = fromuser.nickname.Trim();
        string from_nickname = fromuser.nickname.Trim();

        string title = from_nickname + " decided to follow you from citiport";
        string message = title + "\n------------\n\nYou have a new follower, "+from_nickname + " decided to follow you from citiport\n-------------\nclick the link below for more details:\nhttp://www.citiport.net/user/follow.aspx";



        System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient("210.66.81.108");
        MailAddress from = new MailAddress("service@citiport.net");
        MailAddress to = new MailAddress(touser.email);
        MailMessage mmsg = new MailMessage(from, to);
        mmsg.Body = message;
        mmsg.Subject = title;

        try
        {
            sc.Send(mmsg);
            Response.Write("<script language='javascript'>alert('You have built up a new follow relationship successfully.');window.location.replace('user_profile.aspx?no=" + no + "');</script>");

        }
        catch (Exception e1)
        {
            Response.Write("<script language='javascript'>alert('you have built up a new follow relationship successfully.');window.location.replace('user_profile.aspx?no=" + no + "');</script>");


        }


    }
}
